﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using DataStore.Domain;
using Infrastructure.CurrentData;
using Infrastructure.ViewModels;
using log4net;
using System;

namespace UI
{
	/// <summary>
	/// Interaction logic for KeywordSelector.xaml
	/// </summary>
	public partial class KeywordSelector : UserControl
	{
	    AllKeywordsViewModel _viewModel;
        readonly ILog _log;

        /// <summary>
        /// Occurs when [selection changed].
        /// </summary>
	    public event EventHandler<EventArgs> SelectionChanged;

        /// <summary>
        /// Called when [selection changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnSelectionChanged(object sender, EventArgs e)
        {
            _log.Debug("New keyword selected");

            if(SelectionChanged != null)
            {
                SelectionChanged(sender, e);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="KeywordSelector"/> class.
        /// </summary>
		public KeywordSelector()
		{
			InitializeComponent();

            _log = Current.Container.Resolve<ILog>();

		    LoadKeywordData();
            dataGrid1.RowEditEnding += dataGrid1_RowEditEnding;
		}

        void dataGrid1_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            _log.Debug("Keyword selector row edit ending");

            // Fire event after edit
            if (e.EditAction == DataGridEditAction.Commit)
            {
                OnSelectionChanged(this, EventArgs.Empty);
            }

        }

        /// <summary>
        /// Loads the keyword data.
        /// </summary>
	    void LoadKeywordData()
	    {
            _log.Debug("Loading keywords");

            if (!DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow))
            {
                _viewModel = new AllKeywordsViewModel();
                _viewModel.Loaded  += delegate
                                          {
                                              _log.Debug("Binding keywords");

                                              dataGrid1.ItemsSource = _viewModel.Keywords;
                                              UpdateSelectedItem();
                                          };
            }
	    }

        /// <summary>
        /// Gets the selected keywords viewmodels.
        /// </summary>
        /// <value>The selected keywords VM.</value>
	    public IList SelectedKeywordsVM
	    {
	        get
	        {
	            return dataGrid1.SelectedItems;
	        }
	    }
        ICollection<Keyword> _selected = new List<Keyword>();
        /// <summary>
        /// Gets or sets the selected keywords.
        /// </summary>
        /// <value>The selected keywords.</value>
	    public ICollection<Keyword> SelectedKeywords
	    {
	        get
	        {
                _log.Debug("Returning selected keywords");

                List<Keyword> keywords = new List<Keyword>();
                foreach (KeywordViewModel item in dataGrid1.Items)
                {
                    if (item.Selected)
                    {
                        keywords.Add(item.Keyword);
                    }
                }
                return keywords;
	        }
            set
            {
                _log.Debug("Returning selected keywords");

                _selected = value;
                UpdateSelectedItem();
            }
	    }

        /// <summary>
        /// Updates the selected items.
        /// </summary>
	    void UpdateSelectedItem()
	    {
            _log.Debug("Updating selected keyword");

	        if(_selected == null)
	        {
	            return;
	        }


            foreach (KeywordViewModel item in dataGrid1.Items)
            {
                foreach (Keyword selected in _selected)
                {
                    if (item.Id == selected.Id)
                    {
                        item.Selected = true;
                    }
                }
            }
	    }
	}
}